package com.hnevc.yourhealthmanager.controller;

import com.hnevc.yourhealthmanager.dto.ResponseResult;
import com.hnevc.yourhealthmanager.entity.FoodCategory;
import com.hnevc.yourhealthmanager.entity.FoodMeta;
import com.hnevc.yourhealthmanager.service.FoodMetaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/FoodMeta")
@Api(tags = "收获食物元数据接口")
public class FoodMetaController {
    @Autowired
    private FoodMetaService foodMetaService;

    @PostMapping("/add")
    @ApiOperation(value = "addFoodMeta", notes = "添加食物元数据信息，米饭，辣椒炒肉等")
    public ResponseResult addFoodMeta(@RequestBody  FoodMeta foodMeta) {
        foodMetaService.save(foodMeta);
        return ResponseResult.success(foodMeta);
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "getFoodMetaById", notes = "获取食物元数据信息等")
    public ResponseResult getFoodMetaById(@PathVariable Integer id) {
        FoodMeta categoryServiceById = foodMetaService.getById(id);
        return ResponseResult.success(categoryServiceById);
    }
    @GetMapping("/list")
    @ApiOperation(value = "listFoodMeta", notes = "获取所有食物元数据列表信息")
    public ResponseResult listFoodMeta() {
        List<FoodMeta> foodMetaList = foodMetaService.list();
        return ResponseResult.success(foodMetaList);
    }
    @PutMapping("/update")
    @ApiOperation(value = "updateFoodMeta", notes = "更新食物元数据信息")
    public ResponseResult updateFoodMeta(@RequestBody FoodMeta foodMeta) {
        foodMetaService.updateById(foodMeta);
        return ResponseResult.success(foodMeta);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteFoodMeta", notes = "根据ID删除食物元数据信息")
    public ResponseResult deleteFoodMeta(@PathVariable Integer id) {
        foodMetaService.removeById(id);
        return ResponseResult.success(null);
    }

    @PostMapping("/upload_pic")
    @ApiOperation(value = "上传照片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "照片"),
            @ApiImplicitParam(name = "id",value = "ID")
    })
    public Map<String,Object> uploadPic(@RequestParam("file") MultipartFile file,
                                        @RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            byte[] bytes = file.getBytes();
            String filePath =  new RandomString(10).nextString() + file.getOriginalFilename();
            Path path = Paths.get("D://upload//" +filePath);
            Files.write(path, bytes);
            map.put("success",true);
            map.put("msg","成功");
            //TODO 更新到数据库
            // 1. 获取用户服务对象，
            FoodMeta foodMeta = foodMetaService.updatePic(id, "/" + filePath);
            map.put("data",foodMeta);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","文件读取错误");
        }
        return map;
    }
}
